<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <!-- <el-form-item label="小程序用户id" prop="openid">
        <el-input
          v-model="queryParams.openid"
          placeholder="请输入小程序用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在地区" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入所在地区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in dict.type.app_user_gender"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="平台用户id" prop="unionid">
        <el-input
          v-model="queryParams.unionid"
          placeholder="请输入平台用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="坐标纬度" prop="lat">
        <el-input
          v-model="queryParams.lat"
          placeholder="请输入坐标纬度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="坐标经度" prop="lng">
        <el-input
          v-model="queryParams.lng"
          placeholder="请输入坐标经度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          v-hasPermi="['taskhub:appUser:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['taskhub:appUser:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['taskhub:appUser:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >批量删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          v-hasPermi="['taskhub:appUser:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="appUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="openid" align="center" prop="openid" />
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="头像" align="center" prop="avatar">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.avatar !== null"
            style="width: 100px; height: 100px"
            :src="scope.row.avatar"
            :preview-src-list="[scope.row.avatar]"
          />
        </template>
      </el-table-column>
      <el-table-column label="会员卡" align="center" prop="vipId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_vip_id" :value="scope.row.vipId" />
        </template>
      </el-table-column>
      <el-table-column label="所在地区" align="center" prop="area" />
      <!-- <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_user_gender" :value="scope.row.gender" />
        </template>
      </el-table-column> -->
      <el-table-column label="平台用户id" align="center" prop="unionid" />
      <!-- <el-table-column label="所在城市" align="center" prop="city" /> -->
      <!-- <el-table-column label="所在省份" align="center" prop="province" /> -->
      <!-- <el-table-column label="所在国家" align="center" prop="country" /> -->
      <el-table-column label="坐标纬度" align="center" prop="lat" />
      <el-table-column label="坐标经度" align="center" prop="lng" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['taskhub:userAddr:list']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="oepnPage(scope.row,'/taskhub/userAddr')"
          >设置地址</el-button>
          <el-button
            v-hasPermi="['taskhub:product:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="oepnPage(scope.row,'/taskhub/userCoupon')"
          >设置优惠券</el-button>
          <el-button
            v-hasPermi="['taskhub:appUser:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >详情</el-button>
          <el-button
            v-hasPermi="['taskhub:appUser:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改移动端用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="openid" prop="openid">
          <el-input v-model="form.openid" :disabled="true" placeholder="请输入小程序用户id" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="绑定号码" prop="phone">
          <el-input v-model="form.phone" :disabled="true" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-image
            v-if="form.avatar"
            style="width: 100px; height: 100px"
            :src="form.avatar"
            :preview-src-list="[form.avatar]"
          />
          <el-input v-model="form.avatar" :disabled="true" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="所在地区" prop="area">
          <el-input v-model="form.area" placeholder="请输入所在地区" />
        </el-form-item>
        <!-- <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.app_user_gender"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="全局id" prop="unionid">
          <el-input v-model="form.unionid" placeholder="请输入平台用户id" />
        </el-form-item>
        <el-form-item label="会员卡" prop="vipId">
          <el-select v-model="form.vipId" placeholder="请选择会员卡" clearable>
            <el-option
              v-for="dict in dict.type.app_vip_id"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="所在城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入所在城市" />
        </el-form-item>
        <el-form-item label="所在省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入所在省份" />
        </el-form-item>
        <el-form-item label="所在国家" prop="country">
          <el-input v-model="form.country" placeholder="请输入所在国家" />
        </el-form-item> -->
        <el-form-item label="坐标纬度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入坐标纬度" />
        </el-form-item>
        <el-form-item label="坐标经度" prop="lng">
          <el-input v-model="form.lng" placeholder="请输入坐标经度" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAppUser, getAppUser, delAppUser, addAppUser, updateAppUser } from '@/api/taskhub/appUser'

export default {
  name: 'AppUser',
  dicts: ['app_user_gender', 'app_vip_id'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 移动端用户表格数据
      appUserList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        openid: null,
        nickname: null,
        phone: null,
        area: null,
        gender: null,
        unionid: null,
        lat: null,
        lng: null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phone: [
          { required: true, message: '电话不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询移动端用户列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (this.daterangeCreateTime != null && this.daterangeCreateTime !== '') {
        this.queryParams.params['beginCreateTime'] = this.daterangeCreateTime[0]
        this.queryParams.params['endCreateTime'] = this.daterangeCreateTime[1]
      }
      listAppUser(this.queryParams).then(response => {
        this.appUserList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 带参数跳转
    oepnPage(row, path) {
      this.$router.push({ path: path, query: { userId: row.id, userNick: row.nickname }})
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        openid: null,
        nickname: null,
        phone: null,
        avatar: null,
        area: null,
        gender: null,
        unionid: null,
        city: null,
        province: null,
        country: null,
        lat: null,
        lng: null,
        createTime: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加移动端用户'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getAppUser(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改移动端用户'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAppUser(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addAppUser(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除移动端用户编号为"' + ids + '"的数据项？').then(function() {
        return delAppUser(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('taskhub/appUser/export', {
        ...this.queryParams
      }, `appUser_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
